package com.ketangpai.service.impl;

import com.ketangpai.entity.Student;
import com.ketangpai.entity.Teacher;
import com.ketangpai.mapper.RegisterMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author jiahao
 * @date 2024/2/13 0:13
 */
@Service
public  class RegisterService<T> implements com.ketangpai.service.RegisterService<T> {
    @Autowired
    RegisterMapper registerMapper;
    @Override
    @Transactional
    public int register(T params) {
        if (params instanceof Teacher) {
            int taccount = ((Teacher) params).getTaccount();
            int queryUser = registerMapper.queryUser(taccount);
            if (queryUser != 0) {
                return -1;
            }
            try {
                registerMapper.insertUser(((Teacher) params).getTaccount(),"0");
                return registerMapper.insertTeacher((Teacher) params);
            } catch (Exception e) {
                e.printStackTrace();
                return -1;
            }
        } else if (params instanceof Student) {
            int saccount = ((Student) params).getSaccount();
            int queryUser = registerMapper.queryUser(saccount);
            if (queryUser != 0) {
                return -1;
            }
            try {
                registerMapper.insertUser(((Student) params).getSaccount(),"1");
                return registerMapper.insertStudent((Student) params);
            } catch (Exception e) {
                return -1;
            }
        }
        return 0;
    }
}
